package com.grancalavera.flixel.examples
{
import org.flixel.FlxSprite;
import com.grancalavera.flixel.Utils;
/**
 * This class is an animatd FlxSprite. Using a single PNG image, this class 
 * provides 4 different animations and convenience methods to play them
 */
public class AnimatedSprite extends FlxSprite
{

    //--------------------------------------------------------------------------
    //
    // Constructor
    //
    //--------------------------------------------------------------------------

    /**
     * Creates a new AnimatedSprite instance.
     */
    public function AnimatedSprite(x:Number=0, y:Number=0)
    {
        super(x, y);
        loadGraphic(Skins.SIMPLE_ANIMATION, true, false, 16, 16);
        
        var frameRate:int = 12;
        
        addAnimation(
            "fullLoop", 
            [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1], 
            frameRate, 
            true
        );
        
        addAnimation(
            "backwardLoop", 
            [15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0], 
            frameRate, 
            true
        );
        
        addAnimation(
            "forwardLoop", 
            [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15], 
            frameRate, 
            true
        );
        
        var totalFrames:Number = 60;
        var frames:Array = [];
        while (frames.length < totalFrames)
            frames.push(Utils.randRange(0, (totalFrames - 1)));
        
        addAnimation(
            "randomLoop", 
            frames, 
            frameRate, 
            true
        );
    }

    //--------------------------------------------------------------------------
    //
    //  Methods
    //
    //--------------------------------------------------------------------------

    //----------------------------------
    //  playFullLoop
    //----------------------------------
    /**
     * Plays all the frames on the skin class forward and then backwards. Loops.
     */
    public function playFullLoop():void
    {
        play("fullLoop");    
    } 

    //----------------------------------
    //  playBackwardLoop
    //----------------------------------
    /**
     * Plays all the frames on the skin class backwards. Loops.
     */
    public function playBackwardLoop():void
    {
        play("backwardLoop");   
    }

    //----------------------------------
    //  playForwardLoop
    //----------------------------------
    /**
     * Plays all the frames on the skin class forward. Loops.
     */
    public function playForwardLoop():void
    {
        play("forwardLoop");
    }  

    //----------------------------------
    //  playRandomLoop
    //----------------------------------
    /**
     * Plays a random selection of 60 frames form the skin class. Loops.
     */
    public function playRandomLoop():void
    {
        play("randomLoop");    
    } 
}
}